package cn.com.bluemoon.daps.model.controller;


import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.dto.share.ShareTableIdVo;
import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.service.DapDataModelShareTableService;
import cn.com.bluemoon.daps.model.service.DapDataModelTableService;
import cn.com.bluemoon.daps.model.vo.register.SearchModelRegister;
import cn.com.bluemoon.daps.model.vo.share.SearchShareTableVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统下数仓多模型相互共享表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-09-23
 */
@RestController
@Slf4j
@RequestMapping("/dapModelShare")
@Api(tags = {"模型共享"})
public class DapDataModelShareTableController {

    @Resource
    DapDataModelShareTableService shareService;

    @PostMapping("/list")
    @ApiOperation("共享列表")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = String.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = DapDataModelTable.class)})
    @BmPermission(value = "model:share:list", name = "共享列表")
    public ResultBean<IPage<DapDataModelTable>> tableShareList(@RequestBody @Valid SearchShareTableVo vo){

        return ResultBean.ok(shareService.getShareModelTableList(vo));
    }


    @PostMapping("/do")
    @ApiOperation("表共享")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    @BmPermission(value = "model:table:share", name = "表共享")
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestBody, errorMsg = "只允许模型授权者进行表共享")
    public ResultBean<String> modelTableShare(@RequestBody @Valid ShareTableIdVo tableIds){

        return tableIds.getTableIds().isEmpty() ? ResultBean.error("请选择表共享") :
                shareService.shareTable(tableIds.getTableIds());
    }


    @PostMapping("/cancel")
    @ApiOperation("取消共享")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    @BmPermission(value = "table:share:cancel", name = "表共享")
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestBody, errorMsg = "只允许模型授权者进行取消共享")
    public ResultBean<String> tableShareCancel(@RequestBody @Valid ShareTableIdVo tableIds){

        return tableIds.getTableIds().isEmpty() ? ResultBean.error("请选择取消共享表") :
                shareService.shareTableCancel(tableIds.getTableIds());
    }
}
